﻿$(function () {
    var cardViewThreshold = 580;
    /* Set a locale manually */
    $.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales['en-US']);
    loaddata();
    
    /* Auto adjust the table's view type (table/card) when screen size changes */
    //window.onresize = function () {
    //    $('#table').bootstrapTable('refreshOptions', {
    //        cardView: document.body.clientWidth < cardViewThreshold
    //    });
    //};
});
function reloaddata() {
    //var opt = {
    //    url: "/online/listvacation/"
    //};
    //$('#table').bootstrapTable('refresh', opt);

    $.ajax({
        url: '/online/listuser/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        success: function (data) {
            if (data && data.total > 0) {
                $("#table").bootstrapTable('load', data.rows);
            } else {
                //alert('error');
            }
        }
    })  
}

function loaddata() {
    $.ajax({
        url: '/online/listuser/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },  
        success: function (data) {
            if (data && data.total>0) {
                binddata(data.rows);
                $('.columns-right').append('<button class="btn btn-primary" type="button" data-toggle="modal" data-target="#myModal" name="toggle" aria-label="toggle" title="添加"> 添加 </button>&nbsp;&nbsp;<button class="btn btn-primary" type="button" onclick="opendelconfirm()" style="margin-left:4px;" title="删除"> 删除 </button>&nbsp;&nbsp;<button class="btn btn-primary" type="button" onclick="openeditconfirm()" style="margin-left:4px;" title="编辑"> 编辑 </button>');
                $('.keep-open').hide();
                //getuserlist();//order last one load it.
                //alert('success');
            } else {
                if (data && data.total == -1) {
                    $('#divrole').show().html("<h4> 无权访问，请联系管理员！</h4>");
                }
                //alert('error');
            }
        }
    })
}
var selectrow;
function binddata(datas) {
    var cardViewThreshold = 580;
    $('#table').bootstrapTable({
        cardView: document.body.clientWidth < cardViewThreshold,
        //striped: true,
        search: true,
        pagination: true,
        ageNumber: 1,
        pageSize: 10,
        pageList: [10, 20, 30, 50],
        showColumns: true,
        showToggle: false,
        //showExport: true,
        columns: [{
            field: 'username',
            title: '姓名',
            sortable: true
        }, 
        {
            field: 'useremail',
            title: '邮箱',
            sortable: true
            },
        {
            field: 'temp',
            title: '所属部门',
            sortable: true
        },
        {
            field: 'entrydate',
            title: '入职时间',
            sortable: true
        },
        {
            field: 'userresume',
            title: '简历',
            //sortable: true,
            formatter: aFormatter
        }],
        data: datas
    });

    $('#table').on('click-row.bs.table', function (e, row, element) {
        $('.success').removeClass('success');
        $(element).addClass('success');
        selectrow = row;
        //var index = $('#table').find('tr.success').data('index');
    });
}

function aFormatter(value, row, index) {
    if (value && value.length>0) {
        return ['<a href="/online/file_down?filename=' + value + '">下载</a>'].join("");
    } else {
        return ['<a href="#">暂无</a>'].join("");
    }
    
}

function getuserlist() {
    $.ajax({
        url: '/online/getuserlist/',
        type: 'get',
        dataType: 'json',
        headers: { "X-CSRFToken": getCookie('csrftoken') },
        success: function (data) {
            if (data && data.length > 0) {
                $("#vac_person").empty();
                $("#vac_person2").empty();
                for (var i = 0; i < data.length; i++) {
                    $("#vac_person").append("<option value='" + data[i].USER + "' mail='" + data[i].MAIL + "'>" + data[i].USER + "</option>");
                    $("#vac_person2").append("<option value='" + data[i].USER + "' mail='" + data[i].MAIL + "'>" + data[i].USER + "</option>");
                }
                //alert('success');
            } else {
                //alert('error');
            }
        }
    })
}